package edu.software.PBL_Management_System.demandhistory.controller;

import edu.software.PBL_Management_System.demandhistory.service.DemandHistoryService;
import edu.software.PBL_Management_System.entity.Demand;
import edu.software.PBL_Management_System.entity.DemandHistory;
import edu.software.PBL_Management_System.entity.User;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/demand-history")
public class DemandHistoryController {

    //依赖注入
    @Resource
    private DemandHistoryService demandHistoryService;

    /**
     * 记录脑图更新
     */
    @PostMapping("/mindmap")
    public ResponseEntity<DemandHistory> recordMindMapUpdate(@RequestBody MindMapUpdateRequest request) {
        DemandHistory history = demandHistoryService.recordMindMapUpdate(
                request.getDemand(),
                request.getOldMindMapData(),
                request.getNewMindMapData(),
                request.getModifier()
        );
        return ResponseEntity.ok(history);
    }

    /**
     * 记录描述更新
     */
    @PostMapping("/description")
    public ResponseEntity<DemandHistory> recordDescriptionUpdate(@RequestBody DescriptionUpdateRequest request) {
        DemandHistory history = demandHistoryService.recordDescriptionUpdate(
                request.getDemand(),
                request.getOldDescription(),
                request.getNewDescription(),
                request.getModifier()
        );
        return ResponseEntity.ok(history);
    }

    /**
     * 记录脑图和描述同时更新
     */
    @PostMapping("/both")
    public ResponseEntity<DemandHistory> recordBothUpdate(@RequestBody BothUpdateRequest request) {
        DemandHistory history = demandHistoryService.recordBothUpdate(
                request.getDemand(),
                request.getOldDescription(),
                request.getNewDescription(),
                request.getOldMindMapData(),
                request.getNewMindMapData(),
                request.getModifier(),
                "同时更新了需求描述和脑图"
        );
        return ResponseEntity.ok(history);
    }

    // 请求对象定义
    public static class MindMapUpdateRequest {
        private Demand demand;
        private String oldMindMapData;
        private String newMindMapData;
        private User modifier;

        // Getters and setters
        public Demand getDemand() { return demand; }
        public void setDemand(Demand demand) { this.demand = demand; }
        public String getOldMindMapData() { return oldMindMapData; }
        public void setOldMindMapData(String oldMindMapData) { this.oldMindMapData = oldMindMapData; }
        public String getNewMindMapData() { return newMindMapData; }
        public void setNewMindMapData(String newMindMapData) { this.newMindMapData = newMindMapData; }
        public User getModifier() { return modifier; }
        public void setModifier(User modifier) { this.modifier = modifier; }
    }

    public static class DescriptionUpdateRequest {
        private Demand demand;
        private String oldDescription;
        private String newDescription;
        private User modifier;

        // Getters and setters
        public Demand getDemand() { return demand; }
        public void setDemand(Demand demand) { this.demand = demand; }
        public String getOldDescription() { return oldDescription; }
        public void setOldDescription(String oldDescription) { this.oldDescription = oldDescription; }
        public String getNewDescription() { return newDescription; }
        public void setNewDescription(String newDescription) { this.newDescription = newDescription; }
        public User getModifier() { return modifier; }
        public void setModifier(User modifier) { this.modifier = modifier; }
    }

    public static class BothUpdateRequest {
        private Demand demand;
        private String oldDescription;
        private String newDescription;
        private String oldMindMapData;
        private String newMindMapData;
        private User modifier;

        // Getters and setters
        public Demand getDemand() { return demand; }
        public void setDemand(Demand demand) { this.demand = demand; }
        public String getOldDescription() { return oldDescription; }
        public void setOldDescription(String oldDescription) { this.oldDescription = oldDescription; }
        public String getNewDescription() { return newDescription; }
        public void setNewDescription(String newDescription) { this.newDescription = newDescription; }
        public String getOldMindMapData() { return oldMindMapData; }
        public void setOldMindMapData(String oldMindMapData) { this.oldMindMapData = oldMindMapData; }
        public String getNewMindMapData() { return newMindMapData; }
        public void setNewMindMapData(String newMindMapData) { this.newMindMapData = newMindMapData; }
        public User getModifier() { return modifier; }
        public void setModifier(User modifier) {
            if (modifier != null && modifier.getEmail() != null) {
                modifier.setUsername(modifier.getEmail());
            }
            this.modifier = modifier;
        }
    }

    /**
     * 创建需求历史记录
     */
    @PostMapping
    public ResponseEntity<DemandHistory> createHistory(@RequestBody DemandHistory demandHistory) {
        DemandHistory savedHistory = demandHistoryService.createHistory(demandHistory);
        return ResponseEntity.ok(savedHistory);
    }

    /**
     * 获取指定需求的所有历史记录
     */
    @GetMapping("/demand/{demandId}")
    public ResponseEntity<List<DemandHistory>> getDemandHistory(@PathVariable Long demandId) {
        List<DemandHistory> history = demandHistoryService.getDemandHistory(demandId);
        return ResponseEntity.ok(history);
    }

    /**
     * 获取指定用户的修改历史
     */
    @GetMapping("/user/{modifierId}")
    public ResponseEntity<List<DemandHistory>> getUserHistory(@PathVariable Long modifierId) {
        List<DemandHistory> historyList = demandHistoryService.getUserModificationHistory(modifierId);
        return ResponseEntity.ok(historyList);
    }

    /**
     * 获取指定需求的特定类型的修改历史
     */
    @GetMapping("/demand/{demandId}/type/{modificationType}")
    public ResponseEntity<List<DemandHistory>> getTypeSpecificHistory(
            @PathVariable Long demandId,
            @PathVariable DemandHistory.ModificationType modificationType) {
        List<DemandHistory> historyList = demandHistoryService.getTypeSpecificHistory(demandId, modificationType);
        return ResponseEntity.ok(historyList);
    }

    /**
     * 删除历史记录
     */
    @DeleteMapping("/{historyId}")
    public ResponseEntity<Void> deleteHistory(@PathVariable Long historyId) {
        demandHistoryService.deleteHistory(historyId);
        return ResponseEntity.ok().build();
    }
}
